﻿#include "dockwidgetctd.h"
#include "ui_dockwidgetctd.h"
#include <QDebug>
#include "Template/singleton.h"
#include "SendMessage/confirmdevice.h"
//#include "CTDWidget/dockwidgetctd_imcharts.h"

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include <QMessageBox>
#include <QDateTime>
#include <QButtonGroup>
#include <QFileDialog>
DockWidgetCTD::DockWidgetCTD(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::DockWidgetCTD)
    , m_timerCTD(new QTimer(this))
    // , dockWidgetCTD_IMCharts(DockWidgetCTD_IMChartsInstance)
{
    ui->setupUi(this);

    initConfigCTD_IM();

    m_timerCTD->start(1000);
    connect(m_timerCTD, &QTimer::timeout, this, [&]() {
        // 获取当前时间
        QDateTime currentDateTime = QDateTime::currentDateTime();
        // 格式化时间为字符串
        QString timeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
        // 在标签上显示时间
        ui->m_labelDeviceTime->setText(timeString);
        });

   
}

DockWidgetCTD::~DockWidgetCTD()
{
    delete ui;
}



void DockWidgetCTD::sendBaudAndPortName(int baud,const QString& portName)
{
    // qDebug() << "111111111"<<baud<<"2222222222"<< portName;
    // serialPortCTD
    connect(&serialPortCTD, &QSerialPort::readyRead, this, &DockWidgetCTD::readResponseCTD);
    QByteArray arrMand = "DHI\r\n";
    serialPortCTD.setPortName(portName);
    SetSerialPortParam(&serialPortCTD, baud, arrMand);  // 获取设备信息
}

// 窗口关闭时，更换设备波特率
void DockWidgetCTD::closeDockWidgetDeal()
{
    // 19200
    serialPortCTD.write("BAUD=2\r\n");
    serialPortCTD.write("BAUD=2\r\n");
    //QTimer::singleShot(10, this, [=]() {serialPortCTD.write("BAUD=2\r\n"); });

    int baudRate = serialPortCTD.baudRate();
}

void DockWidgetCTD::readResponseCTD()
{
     //while (serialPortCTD.waitForReadyRead(100))
    //{
        QByteArray readOnce = serialPortCTD.readAll();
        readCTDAll.append(readOnce);
    //}

    qDebug()  << readCTDAll;

    // 读硬件信息
    if (readCTDAll.contains("Supply=") && readCTDAll.endsWith("\r\n>"))
    {
        // qDebug() << "11111111readAll" << readCTDAll;
        auto&& listArrByte = readCTDAll.split('\n');
        for (auto& arrByte : listArrByte)
        {
            QList<QByteArray> listArrData = arrByte.replace('\r', "").split('=');
            if (listArrData.size() > 1)
            {
                for (auto& arrByte : listArrData)
                {
                    if (arrByte.contains("DeviceType"))
                    {
                        ui->label_type->setText(listArrData.back());
                    }
                    else if(arrByte.contains("Serial"))
                    {
                        ui->m_labelSerial->setText(listArrData.back());
                    }
                    else if (arrByte.contains("FirmwareVer"))
                    {
                        ui->m_labelHardWare->setText(listArrData.back());
                    }
                    else if(arrByte.contains("FirmwareTime"))
                    {
                        // 这个字段暂时不知道干什么用的
                        ui->firmwareTime->setText("20" + listArrData.back());

                    }
                    else if(arrByte.contains("MemoryCapacity"))
                    {
                        // 容量
                        ui->memoryCapacity->setText(listArrData.back());
                    }
                    else if (arrByte.contains("Interface"))
                    {
                        // 
                        ui->m_labelSoftwareVersion->setText(listArrData.back());

                    }
                    else if (arrByte.contains("Supply"))
                    {
                        // 电压
                        ui->m_labelVoltage->setText(listArrData.back());
                    }


                }
            }
        }
        readCTDAll.clear();
        serialPortCTD.write("DCI\r\n");   // 信息栏第一长列
    }
    // 读采样设置
    if (readCTDAll.contains("AutoSampleInterval=") && readCTDAll.endsWith("\r\n>"))
    {
        // 初始化波特率
        int baudRate = serialPortCTD.baudRate();
        switch (baudRate)
        {
        case 4800:
            ui->switchBaudRate->setCurrentIndex(0);
            break;
        case 9600:
            ui->switchBaudRate->setCurrentIndex(1);
            break;
        case 19200:
            ui->switchBaudRate->setCurrentIndex(2);
            break;
        case 38400:
            ui->switchBaudRate->setCurrentIndex(3);
            break;
        case 115200:
            ui->switchBaudRate->setCurrentIndex(4);
            break;
        default:
            break;
        }


        auto&& listArrByte = readCTDAll.split('\n');
        for (auto& arrByte : listArrByte)
        {
            QList<QByteArray> listArrData = arrByte.replace('\r', "").split('=');
            if (listArrData.size() > 1)
            {
                // qDebug() << "111111111111111" << listArrData;
                for (auto& arrByte : listArrData)
                {
                    if (arrByte.contains("SyncTrans"))
                    {
                        ui->tableWidget->setItem(0,1,new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(0,2,new QTableWidgetItem("0 And 1"));
                        listArrData.back().contains("1") ? ui->cRealtimeTransfer->setCurrentIndex(0) : ui->cRealtimeTransfer->setCurrentIndex(1);
                    }
                    else if (arrByte.contains("DataFormat"))
                    {
                        QByteArray arrData = listArrData.back();
                        ui->tableWidget->setItem(1, 1, new QTableWidgetItem(arrData.constData()));
                        ui->tableWidget->setItem(1, 2, new QTableWidgetItem("1 And 3"));
                        if (arrData.toInt()==1)
                        {
                            ui->TransFormat->setCurrentIndex(0);
                        }
                        else if (arrData.toInt()==2)
                        {
                            ui->TransFormat->setCurrentIndex(2);
                        }
                        else if (arrData.toInt() == 3)
                        {
                            ui->TransFormat->setCurrentIndex(1);
                        }
                    }
                    else if (arrByte.contains("OutputTemp"))
                    {
                        ui->tableWidget->setItem(2, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(2, 2, new QTableWidgetItem("1"));
                    }
                    else if (arrByte.contains("OutputCond"))
                    {
                        ui->tableWidget->setItem(3, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(3, 2, new QTableWidgetItem("1"));
                    }
                    else if (arrByte.contains("OutputPress"))
                    {
                        ui->tableWidget->setItem(4, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(4, 2, new QTableWidgetItem("1"));
                    }
                    else if (arrByte.contains("OutputDepth"))
                    {
                        ui->tableWidget->setItem(5, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(5, 2, new QTableWidgetItem("None"));
                    }
                    else if (arrByte.contains("OutputSal"))
                    {
                        ui->tableWidget->setItem(6, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(6, 2, new QTableWidgetItem("None"));
                    }
                    else if (arrByte.contains("OutputSV"))
                    {
                        ui->tableWidget->setItem(7, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(7, 2, new QTableWidgetItem("None"));
                    }
                    else if (arrByte.contains("OutputDensity"))
                    {
                        ui->tableWidget->setItem(8, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(8, 2, new QTableWidgetItem("None"));
                    }
                    else if (arrByte.contains("OutputTime"))
                    {
                        ui->tableWidget->setItem(9, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(9, 2, new QTableWidgetItem("1"));
                    }
                    else if (arrByte.contains("MemoryEnable"))
                    {
                        QByteArray arrData = listArrData.back();
                        ui->tableWidget->setItem(10, 1, new QTableWidgetItem(arrData.constData()));
                        ui->tableWidget->setItem(10, 2, new QTableWidgetItem("0 And 1"));

                        arrData.contains("1") ? ui->switchStorage->setCurrentIndex(0) : ui->switchStorage->setCurrentIndex(1);

                    }
                    else if (arrByte.contains("LowPowerMode"))
                    {
                        ui->tableWidget->setItem(11, 1, new QTableWidgetItem(listArrData.back().constData()));
                        ui->tableWidget->setItem(11, 2, new QTableWidgetItem("1"));
                    }
                    else if (arrByte.contains("AutoSampleInterval"))
                    {
                        QByteArray  arrData = listArrData.back();
                        ui->tableWidget->setItem(12, 1, new QTableWidgetItem(arrData.constData()));
                        ui->tableWidget->setItem(12, 2, new QTableWidgetItem("1-86400s"));
                        QString strData = QString(arrData).left(arrData.size() - 1);
                        int  sampleInterval = strData.toInt();
                        int iHour = sampleInterval / 3600;
                        int iMin = sampleInterval % 3600 / 60;
                        int iSec = sampleInterval % 60;
                        if (iHour > 0)
                        {
                            ui->m_radioButton_Hour->setChecked(true);
                            ui->m_spinBox_Hour->setEnabled(true);
                            ui->m_spinBox_Minute->setEnabled(true);
                            ui->m_spinBox_Second->setEnabled(true);
                        }
                        else if (iMin > 0)
                        {
                            ui->m_radioButton_Minute->setChecked(true);
                            ui->m_spinBox_Second->setEnabled(true);
                            ui->m_spinBox_Minute->setEnabled(true);
                            ui->m_spinBox_Hour->setEnabled(false);
                        }
                        else if (iSec > 0)
                        {
                            ui->m_radioButton_Second->setChecked(true);
                            ui->m_spinBox_Second->setEnabled(true);
                            ui->m_spinBox_Minute->setEnabled(false);
                            ui->m_spinBox_Hour->setEnabled(false);

                        }
                        ui->m_spinBox_Hour->setValue(iHour);
                        ui->m_spinBox_Minute->setValue(iMin);
                        ui->m_spinBox_Second->setValue(iSec);

                    }
                }
            }
        }

        readCTDAll.clear();
        serialPortCTD.write("GETCC\r\n");  // 获取校准系数
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("GETCC\r\n"); });
    }

    // 读系数
    if (readCTDAll.contains("C4=") && readCTDAll.endsWith("\r\n>"))
    {
        auto&& listArrByte = readCTDAll.split('\n');
        for (auto& arrByte : listArrByte)
        {
            // qDebug() << "arrByte:" << arrByte.replace('\r', "").split('=');

            QList<QByteArray> listArrData = arrByte.replace('\r', "").split('=');
            // 单独处理压力后面的参数
            if (listArrData.at(0).contains("Pressure"))
            {
                QList<QByteArray> arrData = listArrData.at(0).split(':');
                for (auto& arrByte : arrData)
                {
                    if (arrByte.contains("Pressure"))
                    {
                        // 压力后面的数值
                        Pressure = arrData.back();
                    }
                }
            }
            
            // 处理系数
            if (listArrData.size() > 1)
            {
                for (auto& arrByte : listArrData)
                {
                    // 温度
                    if (arrByte.contains("T1"))
                    {
                        T1 = listArrData.back();
                    }
                    else if (arrByte.contains("T2"))
                    {
                        T2 = listArrData.back();
                    }
                    else if (arrByte.contains("T3"))
                    {
                        T3 = listArrData.back();
                    }
                    else if (arrByte.contains("T4"))
                    {
                        T4 = listArrData.back();
                    }
                    // 压力
                    if (arrByte.contains("P1"))
                    {
                        P1 = listArrData.back();
                    }
                    else if (arrByte.contains("P2"))
                    {
                        P2 = listArrData.back();
                    }
                    else if (arrByte.contains("P3"))
                    {
                        P3 = listArrData.back();
                    }
                    else if (arrByte.contains("P4"))
                    {
                        P4 = listArrData.back();
                    }
                    else if (arrByte.contains("Poffset"))
                    {
                        Poffset = listArrData.back();
                    }
                    else if (arrByte.contains("Latitude"))
                    {
                        Latitude = listArrData.back();
                    }

                    // 电导率
                    else if (arrByte.contains("C1"))
                    {
                        C1 = listArrData.back();
                    }
                    else if (arrByte.contains("C2"))
                    {
                        C2 = listArrData.back();
                    }
                    else if (arrByte.contains("C3"))
                    {
                        C3 = listArrData.back();
                    }
                    else if (arrByte.contains("C4"))
                    {
                        C4 = listArrData.back();
                    }
                }
            }
        }

        // 设置校准参数选中第0个
        //ui->m_combomBox_Coefficient->setCurrentIndex(0);
        if (ui->m_combomBox_Coefficient->currentIndex() == 0)
        {
            // 走设置温度的系数
            setCalibrationCoefficientTemperatureTable();
        }
        if (ui->m_combomBox_Coefficient->currentIndex() == 1)
        {
            // 走设置压力的系数
            setCalibrationCoefficientPressureTable();
        }
        if (ui->m_combomBox_Coefficient->currentIndex() == 2)
        {
            // 走设置电导率的系数
            setCalibrationCoefficientConductivityTable();
        }


        readCTDAll.clear();

        // 下发读状态指令
        serialPortCTD.write("DSI\r\n");  // 采样条数
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DSI\r\n"); });
    }
    // 读状态
    if (readCTDAll.contains("Status=") && readCTDAll.endsWith("\r\n>"))
    {
        // CTD-0618IM v1.0.0a SN100227\r\nTime=23-08-11,14:26:17\r\nSamples=571\r\nStatus=stopped!\r\n>
        auto&& listArrByte = readCTDAll.split('\n');
        for (auto& arrByte : listArrByte)
        {
            QList<QByteArray> listArrData = arrByte.replace('\r', "").split('=');
            if (listArrData.size() > 1)
            {
                for (auto& arrByte : listArrData)
                {
                    if (arrByte.contains("Time"))
                    {
                        ui->m_labelDeviceTime->setText("20"+listArrData.back().replace(',', " "));
                    }
                    else if (arrByte.contains("Samples"))
                    {
                        QByteArray arrData = listArrData.back();
                        ui->sampleCount->setText(arrData +" 次");
                        // 采样次数赋值给进度条
                        m_iSamplesQuantity = arrData.toInt();
                        // 将内存进度条设置成 0
                        int result = arrData.toInt() / 8388496;
                        ui->m_proBar_Memory->setFormat(QString("已使用内存 %%1").arg(result));     // 只显示进度条上的文字 (条数/总条数)
                        ui->m_proBar_Memory->setValue(result);                                     // 只显示进度条上的值 (条数/总条数)
                    }
                    else if (arrByte.contains("Status"))
                    {
                        QByteArray arrData = listArrData.back();
                        // 后面那个参数是计划 2-90 分钟后采样的状态
                        if (arrData.contains("logging") || arrData.contains("enable"))
                        {
                            if (arrData.contains("enable"))
                            {
                                ui->m_labelStatus->setText("  waiting to start");
                            }
                            else
                            {
                                ui->m_labelStatus->setText("  " + arrData);
                            }
                            isStartSample = false;
                            ui->pushButtonStartSample->setText("停止");
                            // 设置按钮灰掉
                            setChangeStatus(false);
                        }

                        else if (arrData.contains("stopped"))
                        {
                            ui->m_labelStatus->setText("  " + arrData);
                            isStartSample = true;
                            ui->pushButtonStartSample->setText("启动");
                            setChangeStatus(true);
                        }
                    }

                }
            }
        }
        readCTDAll.clear();
    }

    // 下发时间间隔
    if (readCTDAll.contains("Interval") && readCTDAll.endsWith("\r\n>"))
    {
        // 收到时间间隔   Interval=4\r\n>
        qDebug() << "1111111111" << readCTDAll;
        // 更改时间间隔
        auto&& listArrByte = readCTDAll.split('=');
        QByteArray arrData = listArrByte.back().replace('\r', "").replace('\n', "").replace('>', "");
        int sampleInterval = arrData.toInt();
        int iSec = sampleInterval % 60;
        int iHour = sampleInterval / 3600;
        int iMin = sampleInterval % 3600 / 60;
        if (iHour > 0)
        {
            ui->m_radioButton_Hour->setChecked(true);
            ui->m_spinBox_Hour->setEnabled(true);
            ui->m_spinBox_Minute->setEnabled(true);
            ui->m_spinBox_Second->setEnabled(true);
        }
        else if (iMin > 0)
        {
            ui->m_radioButton_Minute->setChecked(true);
            ui->m_spinBox_Second->setEnabled(true);
            ui->m_spinBox_Minute->setEnabled(true);
            ui->m_spinBox_Hour->setEnabled(false);
        }
        else if (iSec > 0)
        {
            ui->m_radioButton_Second->setChecked(true);
            ui->m_spinBox_Second->setEnabled(true);
            ui->m_spinBox_Minute->setEnabled(false);
            ui->m_spinBox_Hour->setEnabled(false);

        }
        ui->m_spinBox_Hour->setValue(iHour);
        ui->m_spinBox_Minute->setValue(iMin);
        ui->m_spinBox_Second->setValue(iSec);
        // 以上设置返回的时间间隔
        
        // 下发采样指令
        // 十秒钟之后采样
        if (ui->m_radio_LocalDateTime->isChecked())
        {
            // 下发采样值指令
            serialPortCTD.write("STA\r\n");
        }

        // 定时 2-90 分钟后启动
        else if (ui->radioButton->isChecked())
        {
            // qDebug() << "11111" << ui->m_labelDeviceTime->text().mid(2).remove('-').remove(':').remove(' ').toUtf8();
            // qDebug() << "11111" << ui->m_labelDeviceTime->text();

            QString Get_Hour = ui->m_labelDeviceTime->text().mid(11, 2);
            QString Get_Min = ui->m_labelDeviceTime->text().mid(14, 2);
            QString Get_Sec = ui->m_labelDeviceTime->text().mid(17, 2);

            quint16 Set_Hour = ui->m_pBoxHour->value();
            quint16 Set_Min = ui->m_pBoxMin->value();
            quint16 Set_Time = 0;

            if (Get_Hour.toUInt() > Set_Hour)//跨天 S<G
            {
                Set_Time = ((Set_Hour + 24 - Get_Hour.toUInt()) * 60) + Set_Min - Get_Min.toUInt();
            }
            else if (Get_Hour.toUInt() == Set_Hour)//S=G
            {
                Set_Time = Set_Min - Get_Min.toUInt();
            }
            else  //S>G
            {
                Set_Time = ((Set_Hour - Get_Hour.toUInt()) * 60) + Set_Min - Get_Min.toUInt();
            }
            if ((Set_Time < 2) || (Set_Time > 90))
            {
                QMessageBox::information(this, tr("prompt"), tr("启动时间错误，需在 2-90 分钟"));
                return;
            }
            // 下发采样间隔指令
            // qDebug() << "11111111" << "STA=" + QByteArray::number(Set_Time) + "\r\n";
            serialPortCTD.write("STA="+ QByteArray::number(Set_Time)+"\r\n");

        }

        readCTDAll.clear();
    }

    // 更改状态
    if (readCTDAll.contains("Start to record in"))
    {
        // 说明正在采样
        ui->m_labelStatus->setText("  logging!");
        //ui->m_labelStatus->setText("  " + readCTDAll.replace('\r', "").replace('\n', "").replace('>', ""));

        isStartSample = false;
        ui->pushButtonStartSample->setText("停止");
        // 更改状态
        if (!isStartSample)
        {
            setChangeStatus(false);
        }
        
        readCTDAll.clear();
    }



    // 实时传输
    if (readCTDAll.contains("SyncTrans=") && readCTDAll.endsWith("\r\n>"))
    {
        auto&& listArrByte = readCTDAll.split('=');
        if (listArrByte.size() > 1)
        {
            listArrByte.back().contains("1") ? ui->cRealtimeTransfer->setCurrentIndex(0) : ui->cRealtimeTransfer->setCurrentIndex(1);
        }
        readCTDAll.clear();
    }

    // 数据传输格式
    if (readCTDAll.contains("DataFormat=") && readCTDAll.endsWith("\r\n>"))
    {
        // DataFormat=3
        auto&& listArrByte = readCTDAll.split('=');
        if (listArrByte.size() > 1)
        {
            QByteArray arrData = listArrByte.back();

            if (arrData.toInt() == 1)
            {
                ui->TransFormat->setCurrentIndex(0);
            }
            else if (arrData.toInt() == 2)
            {
                ui->TransFormat->setCurrentIndex(2);
            }
            else if (arrData.toInt() == 3)
            {
                ui->TransFormat->setCurrentIndex(1);
            }
        }
        readCTDAll.clear();
    }

    // 使能存储
    if (readCTDAll.contains("Memory=") && readCTDAll.endsWith("\r\n>"))
    {
        auto&& listArrByte = readCTDAll.split('=');
        if (listArrByte.size() > 1)
        {
            if (listArrByte.back().contains("1"))
            {
                ui->switchStorage->setCurrentIndex(0);
            }
            else
            {
                ui->switchStorage->setCurrentIndex(1);
            }
        }
        readCTDAll.clear();
	}

	//切换波特率 Switching baud to 19200
	if (readCTDAll.contains("Switching baud to") && readCTDAll.endsWith("\r\n>"))
	{
        auto&& listArrByte = readCTDAll.split(' ');
        if (listArrByte.size() > 3)
        {
            int baudRate = listArrByte.at(3).toInt();
            switch (baudRate)
            {
            case 4800:
                // 更换串口波特率 4800
                serialPortCTD.setBaudRate(4800);
                ui->switchBaudRate->setCurrentIndex(0);
                break;
            case 9600:
                // 更换串口波特率 9600
                serialPortCTD.setBaudRate(9600);
                ui->switchBaudRate->setCurrentIndex(1);
                break;
            case 19200:
                // 更换串口波特率 19200
                serialPortCTD.setBaudRate(19200);
                ui->switchBaudRate->setCurrentIndex(2);
                break;
            case 38400:
                // 更换串口波特率 38400
                serialPortCTD.setBaudRate(38400);
                ui->switchBaudRate->setCurrentIndex(3);
                break;
            case 115200:
                // 更换串口波特率 115200
                serialPortCTD.setBaudRate(115200);
                ui->switchBaudRate->setCurrentIndex(4);
                break;
            default:
                break;
            }
        }
        readCTDAll.clear();
	}



    // 处理内存清空事件
    if (readCTDAll.contains("Memory has been re-initialized!"))
    {
        // 设置样式
        ui->m_proBar_Memory->setStyleSheet("QProgressBar{border-radius:5px;border:1px;background:rgb(0, 255, 0);} QProgressBar::chunk{border-radius:5px;background:rgb(255, 127, 0)}");

        // 采样次数设置成 0
        ui->sampleCount->setText("0 次");
        // 将内存进度条设置成 0
        ui->m_proBar_Memory->setFormat(QString("已使用内存 %%1").arg(0));     // 只显示进度条上的文字  (条数/总条数)
        ui->m_proBar_Memory->setValue(0);                                     // 只显示进度条上的值  (条数/总条数)

        readCTDAll.clear();
    }


    // 停止采样按钮对应的响应事件
    if (readCTDAll.contains("Stop logging") || readCTDAll.contains("Logger already stopped"))
    {
        
        // 更改状态
        //ui->m_labelStatus->setText(readCTDAll.replace('\r',"").replace('\n',"").replace('>',""));
        ui->m_labelStatus->setText("  stopped!");
        // 更改按钮
        isStartSample = true;
        ui->pushButtonStartSample->setText("启动");

        if (isStartSample)
        {
            setChangeStatus(true);
        }

        readCTDAll.clear();
    }

    // 打点
	if (readCTDAll.contains("$PRMSE") && readCTDAll.endsWith("\r\n>"))
	{
		// 打点
		emit sendOnceSample(readCTDAll);
        readCTDAll.clear();
	}

    else if (readCTDAll.contains("e11 ok") && readCTDAll.endsWith("\r\n>"))
    {
        dealTableE11();
    }
    else if (readCTDAll.contains("e12 ok") && readCTDAll.endsWith("\r\n>"))
    {
        dealTableE12();
    }
    else if (readCTDAll.contains("e13 ok") && readCTDAll.endsWith("\r\n>"))
    {
        dealTableE13();
    }
    else if (readCTDAll.contains("e14 ok") && readCTDAll.endsWith("\r\n>"))
    {
        readCTDAll.clear();
    }

    else if (readCTDAll.contains("e21 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        dealTableE21();
    }
    else if (readCTDAll.contains("e22 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        dealTableE22();
    }
    else if (readCTDAll.contains("e23 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        dealTableE23();
    }
    else if (readCTDAll.contains("e24 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        readCTDAll.clear();
    }

    // Pressure
    else if (readCTDAll.contains("e31 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        dealTableE31();
    }
    else if (readCTDAll.contains("e32 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        dealTableE32();
    }
    else if (readCTDAll.contains("e33 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        dealTableE33();
    }
    else if (readCTDAll.contains("e34 ok") && readCTDAll.endsWith("\r\n>"))
    {
        // 下发下一条指令
        readCTDAll.clear();
    }

    else if (readCTDAll.contains("Invalid parameters!\r\n>"))
    {
        readCTDAll.clear();
    }

    // 下载 
    if (CTDIMDownLoad)
    {
        // qDebug() << "1111111111111" << readCTDAll;
        // 选择文件开始下载数据
        if (!m_clsFile.isOpen())
        {
            m_clsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
        }
        
        qint64 writeSize = m_clsFile.write(readCTDAll);
        m_iSamplingCount += readCTDAll.count("\r\n");
        m_pBar->setValue(m_iSamplingCount * 100.0 / (m_iSamplesQuantity + 2));

        if (readCTDAll.contains(">"))
        {
            m_pBar->setValue(100);
            m_clsFile.close();
            m_iSamplingCount = 0;
            CTDIMDownLoad = false;
        }
        readCTDAll.clear();
    }
    // readCTDAll.clear();
}

void DockWidgetCTD::initConfigCTD_IM()
{
    this->setFeatures(QDockWidget::DockWidgetMovable);
    this->setAllowedAreas(Qt::RightDockWidgetArea);
    this->setContentsMargins(0, 0, 0, 0);
    //setFont(QFont("Microsoft YaHei UI", 8, 70));
    this->setWindowTitle(tr("CTD_IM"));
    // 设置状态栏黄色
    ui->m_labelStatus->setStyleSheet("QLabel{background:#ffff00; border-radius: 8px;}");
    // 时间编辑栏显示 年月日时分秒
    //ui->m_dateTimeEdit->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
    //ui->m_dateTimeEdit_End->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
    ui->m_spinBox_Hour->setRange(0, 23);
    ui->m_spinBox_Minute->setRange(0, 59);
    ui->m_spinBox_Second->setRange(0, 59);
    ui->m_proBar_Memory->setStyleSheet("QProgressBar{border-radius:5px;border:1px;background:rgb(0, 255, 0);} QProgressBar::chunk{border-radius:5px;background:rgb(255, 127, 0)}");

    //ui->m_proBar_Memory->setFormat(QString("已使用内存 %%1").arg((100 * m_iPageQuantityTSC / 8192) == 0 ? 1 : (100 * m_iPageQuantityTSC / 8192)));

    ui->m_proBar_Memory->setAlignment(Qt::AlignLeft | Qt::AlignCenter);
    // 第三页 参数 下拉框选中第一个
    ui->m_combomBox_Coefficient->setCurrentIndex(0);

    // 设置TableWidget不可编辑
    for (int index = 0; index < 4; index++)
    {
        ui->m_tableWidget_CalibrationT->item(index, 0)->setFlags(ui->m_tableWidget_CalibrationT->item(index, 0)->flags() & ~Qt::ItemIsEditable);
        //  设置字体
        // ui->m_tableWidget_CalibrationT->item(index,0)->setFont(QFont("微软雅黑",9,QFont::Normal));
        // 设置字体颜色
        // ui->m_tableWidget_CalibrationT->item(index,0)->setTextColor(Qt::darkGray);
        // 设置 背景色
        // ui->m_tableWidget_CalibrationT->item(index,0)->setBackground(QBrush(QColor(Qt::lightGray)));
    }
    ui->m_tableWidget_CalibrationT->setFocusPolicy(Qt::NoFocus);
    //表头标题用QStringList来表示
    QStringList headerText;
    headerText << "标 签" << "数 值";
    ui->m_tableWidget_CalibrationT->setHorizontalHeaderLabels(headerText);
    ui->m_tableWidget_CalibrationT->setColumnWidth(1, 150);
    ui->m_tableWidget_CalibrationT->setColumnWidth(2, 50);
    //设置最后一栏自适应长度
    ui->m_tableWidget_CalibrationT->horizontalHeader()->setStretchLastSection(true);

    ui->tabWidget->setTabText(0, "配置");
    ui->tabWidget->setTabText(1, "信息");
    ui->tabWidget->setTabText(2, "校准");
    ui->tabWidget->setTabText(3, "参数");
    // 设置按钮不可用
    ui->tabWidget->setTabEnabled(3, false);
    ui->tabWidget->setTabToolTip(3, QString("正在开发中"));
    ui->tabWidget->setCurrentIndex(0);

    // 设置RadioButton互斥
    QButtonGroup* buttonGroup = new QButtonGroup();
    buttonGroup->addButton(ui->m_radio_LocalDateTime);
    buttonGroup->addButton(ui->radioButton);

    // QRegExp regexp;
    // regexp.setPattern("^[0-9]+$");
    // ui->lineEditMinute->setValidator(new QRegExpValidator(regexp));
   
    //ui->lineEditMinute->setValidator(new QIntValidator(2, 90,this));

    ui->m_radio_LocalDateTime->setChecked(true);
    ui->radioButton->setChecked(false);
}

void DockWidgetCTD::setChangeStatus(bool flag)
{
    // 更改本类按钮状态
    ui->getStatusButton->setEnabled(flag);
    ui->mCTDIMDownLoad->setEnabled(flag);
    ui->clearMemory->setEnabled(flag);
    ui->mUseLastConfig->setEnabled(flag);
    ui->saveRealtimeTransfer->setEnabled(flag);
    ui->saveTransFormat->setEnabled(flag);
    ui->saveStorage->setEnabled(flag);
    ui->saveBaudRate->setEnabled(flag);
    ui->getConfig->setEnabled(flag);
    ui->m_PtnSaveCoefficient->setEnabled(flag);
    ui->m_CalibrationCoefficient->setEnabled(flag);
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    // 将下面打点按钮也灰掉-> 发信号
    emit sendChangeStatus(flag);
}

void DockWidgetCTD::setCalibrationCoefficientTemperatureTable()
{
    ui->m_tableWidget_CalibrationT->setRowCount(4);
    ui->m_tableWidget_CalibrationT->setItem(0, 0, new QTableWidgetItem("CoefficientA"));
    ui->m_tableWidget_CalibrationT->setItem(0, 1, new QTableWidgetItem(T1));
    ui->m_tableWidget_CalibrationT->setItem(0, 2, new QTableWidgetItem("HCS11"));
    ui->m_tableWidget_CalibrationT->setItem(1, 0, new QTableWidgetItem("CoefficientB"));
    ui->m_tableWidget_CalibrationT->setItem(1, 1, new QTableWidgetItem(T2));
    ui->m_tableWidget_CalibrationT->setItem(1, 2, new QTableWidgetItem("HCS12"));
    ui->m_tableWidget_CalibrationT->setItem(2, 0, new QTableWidgetItem("CoefficientC"));
    ui->m_tableWidget_CalibrationT->setItem(2, 1, new QTableWidgetItem(T3));
    ui->m_tableWidget_CalibrationT->setItem(2, 2, new QTableWidgetItem("HCS13"));
    ui->m_tableWidget_CalibrationT->setItem(3, 0, new QTableWidgetItem("CoefficientD"));
    ui->m_tableWidget_CalibrationT->setItem(3, 1, new QTableWidgetItem(T4));
    ui->m_tableWidget_CalibrationT->setItem(3, 2, new QTableWidgetItem("HCS14"));

    // 清空
    ui->m_tableWidget_CalibrationT->setItem(0,3,new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(1,3,new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(2,3,new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(3,3,new QTableWidgetItem(""));

}

void DockWidgetCTD::setCalibrationCoefficientPressureTable()
{
    ui->m_tableWidget_CalibrationT->setRowCount(7);
    ui->m_tableWidget_CalibrationT->setColumnWidth(2, 80);


    // 走设置压力的系数
    ui->m_tableWidget_CalibrationT->setItem(0, 0, new QTableWidgetItem("CoefficientA"));
    ui->m_tableWidget_CalibrationT->setItem(0, 1, new QTableWidgetItem(P1));
    ui->m_tableWidget_CalibrationT->setItem(0, 2, new QTableWidgetItem("HCS21"));
    ui->m_tableWidget_CalibrationT->setItem(1, 0, new QTableWidgetItem("CoefficientB"));
    ui->m_tableWidget_CalibrationT->setItem(1, 1, new QTableWidgetItem(P2));
    ui->m_tableWidget_CalibrationT->setItem(1, 2, new QTableWidgetItem("HCS22"));
    ui->m_tableWidget_CalibrationT->setItem(2, 0, new QTableWidgetItem("CoefficientC"));
    ui->m_tableWidget_CalibrationT->setItem(2, 1, new QTableWidgetItem(P3));
    ui->m_tableWidget_CalibrationT->setItem(2, 2, new QTableWidgetItem("HCS23"));
    ui->m_tableWidget_CalibrationT->setItem(3, 0, new QTableWidgetItem("CoefficientD"));
    ui->m_tableWidget_CalibrationT->setItem(3, 1, new QTableWidgetItem(P4));
    ui->m_tableWidget_CalibrationT->setItem(3, 2, new QTableWidgetItem("HCS24"));

    ui->m_tableWidget_CalibrationT->setItem(4, 0, new QTableWidgetItem("Poffset"));
    ui->m_tableWidget_CalibrationT->setItem(4, 1, new QTableWidgetItem(Poffset));
    ui->m_tableWidget_CalibrationT->setItem(4, 2, new QTableWidgetItem("Poffset"));
    ui->m_tableWidget_CalibrationT->setItem(4, 3, new QTableWidgetItem(Poffset));

    ui->m_tableWidget_CalibrationT->setItem(5, 0, new QTableWidgetItem("Latitude"));
    ui->m_tableWidget_CalibrationT->setItem(5, 1, new QTableWidgetItem(Latitude));
    ui->m_tableWidget_CalibrationT->setItem(5, 2, new QTableWidgetItem("Latitude"));
    ui->m_tableWidget_CalibrationT->setItem(5, 3, new QTableWidgetItem(Latitude));

    ui->m_tableWidget_CalibrationT->setItem(6, 0, new QTableWidgetItem("Pressure"));
    ui->m_tableWidget_CalibrationT->setItem(6, 1, new QTableWidgetItem(Pressure));
    ui->m_tableWidget_CalibrationT->setItem(6, 2, new QTableWidgetItem("Pressure"));
    ui->m_tableWidget_CalibrationT->setItem(6, 3, new QTableWidgetItem(Pressure));

    // 清空
    ui->m_tableWidget_CalibrationT->setItem(0, 3, new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(1, 3, new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(2, 3, new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(3, 3, new QTableWidgetItem(""));
}

void DockWidgetCTD::setCalibrationCoefficientConductivityTable()
{
    ui->m_tableWidget_CalibrationT->setRowCount(4);
    ui->m_tableWidget_CalibrationT->setItem(0, 0, new QTableWidgetItem("CoefficientA"));
    ui->m_tableWidget_CalibrationT->setItem(0, 1, new QTableWidgetItem(C1));
    ui->m_tableWidget_CalibrationT->setItem(0, 2, new QTableWidgetItem("HCS31"));

    ui->m_tableWidget_CalibrationT->setItem(1, 0, new QTableWidgetItem("CoefficientB"));
    ui->m_tableWidget_CalibrationT->setItem(1, 1, new QTableWidgetItem(C2));
    ui->m_tableWidget_CalibrationT->setItem(1, 2, new QTableWidgetItem("HCS32"));

    ui->m_tableWidget_CalibrationT->setItem(2, 0, new QTableWidgetItem("CoefficientC"));
    ui->m_tableWidget_CalibrationT->setItem(2, 1, new QTableWidgetItem(C3));
    ui->m_tableWidget_CalibrationT->setItem(2, 2, new QTableWidgetItem("HCS33"));

    ui->m_tableWidget_CalibrationT->setItem(3, 0, new QTableWidgetItem("CoefficientD"));
    ui->m_tableWidget_CalibrationT->setItem(3, 1, new QTableWidgetItem(C4));
    ui->m_tableWidget_CalibrationT->setItem(3, 2, new QTableWidgetItem("HCS34"));

    // 清空
    ui->m_tableWidget_CalibrationT->setItem(0, 3, new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(1, 3, new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(2, 3, new QTableWidgetItem(""));
    ui->m_tableWidget_CalibrationT->setItem(3, 3, new QTableWidgetItem(""));

}



bool DockWidgetCTD::SetSerialPortParam(QSerialPort* serial, int Baudrate, QByteArray serialCommand)
{
    serial->setReadBufferSize(0);
    serial->setBaudRate(Baudrate);
    serial->setDataBits(QSerialPort::Data8);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setParity(QSerialPort::NoParity);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->close();
    if (!serial->open(QIODevice::ReadWrite))
    {
        qDebug() << "not is open";
        //QMessageBox::information(NULL, "TD-SC", "端口号打开失败",QMessageBox::Ok);
        return false;
    }
    serial->write(serialCommand);
    qDebug() << serialCommand;
    return true;
}



void DockWidgetCTD::on_m_combomBox_Coefficient_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    if (ui->m_combomBox_Coefficient->currentIndex() == 0)
    {
        setCalibrationCoefficientTemperatureTable();
    }
    if (ui->m_combomBox_Coefficient->currentIndex() == 1)
    {
        setCalibrationCoefficientPressureTable();
    }
    if (ui->m_combomBox_Coefficient->currentIndex() == 2)
    {
        setCalibrationCoefficientConductivityTable();
    }
}


void DockWidgetCTD::on_pushButton_3_clicked()
{
    serialPortCTD.write("INI\r\n");
    QTimer::singleShot(500, this, [=]() {serialPortCTD.write("INI\r\n"); });
}


void DockWidgetCTD::on_m_Button_SyncLocalTime_clicked()
{
    // 获取当前系统时间
    QDateTime currentDateTime = QDateTime::currentDateTime();

    // 输出当前系统时间
    qDebug() << "Current System Time: " << currentDateTime.toString("yyMMddhhmmss");
    QString sendTime = "TIME=" + currentDateTime.toString("yyMMddhhmmss") + "\r\n";

    //  TIME = 230711223344
    serialPortCTD.write(sendTime.toUtf8());
    QTimer::singleShot(500, this, [=]() { serialPortCTD.write(sendTime.toUtf8()); });

}


void DockWidgetCTD::on_getStatusButton_clicked()
{
    // 点击事件获取状态
    serialPortCTD.write("DSI\r\n");

}





void DockWidgetCTD::on_saveRealtimeTransfer_clicked()
{
    if (ui->cRealtimeTransfer->currentIndex() == 0)
    {

        serialPortCTD.write("SYNCTRANS=1\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("SYNCTRANS=1\r\n"); });
    }
    else
    {
        serialPortCTD.write("SYNCTRANS=0\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("SYNCTRANS=0\r\n"); });
    }


}

void DockWidgetCTD::on_saveTransFormat_clicked()
{
    // 标准十进制
    if (ui->TransFormat->currentIndex()==0)
    {
        serialPortCTD.write("DATAFORMAT=1\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DATAFORMAT=1\r\n"); });
    }
    // 十进制
    else if(ui->TransFormat->currentIndex() == 1)
    {
        serialPortCTD.write("DATAFORMAT=3\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DATAFORMAT=3\r\n"); });
    }
    // 16进制
    else if (ui->TransFormat->currentIndex() == 2)
    {
        serialPortCTD.write("DATAFORMAT=2\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DATAFORMAT=2\r\n"); });
    }
}

void DockWidgetCTD::on_saveStorage_clicked()
{
    if (ui->switchStorage->currentIndex() == 0)
    {
        // 下发 开指令
        serialPortCTD.write("MEMORY=1\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("MEMORY=1\r\n"); });
    }
    else
    {
        // 下发 关指令
        serialPortCTD.write("MEMORY=0\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("MEMORY=0\r\n"); });
    }



}


void DockWidgetCTD::on_saveBaudRate_clicked()
{
    if (ui->switchBaudRate->currentIndex() == 0)
    {
        // 4800
        serialPortCTD.write("BAUD=0\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("BAUD=0\r\n"); });
    }
    else if (ui->switchBaudRate->currentIndex() == 1)
    {
        // 9600
        serialPortCTD.write("BAUD=1\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("BAUD=1\r\n"); });
    }
    else if (ui->switchBaudRate->currentIndex() == 2)
    {
        // 19200
        serialPortCTD.write("BAUD=2\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("BAUD=2\r\n"); });
    }
    else if (ui->switchBaudRate->currentIndex() ==3)
    {
        // 38400
        serialPortCTD.write("BAUD=3\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("BAUD=3\r\n"); });
    }
    else if (ui->switchBaudRate->currentIndex() == 4)
    {
        // 38400
        serialPortCTD.write("BAUD=4\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("BAUD=4\r\n"); });
    }
    else if (ui->switchBaudRate->currentIndex() == 5)
    {
        // 115200
        serialPortCTD.write("BAUD=5\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("BAUD=5\r\n"); });
    }


}


void DockWidgetCTD::on_getConfig_clicked()
{
    serialPortCTD.write("DCI\r\n");
    QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DCI\r\n"); });
}


void DockWidgetCTD::on_clearMemory_clicked()
{
    serialPortCTD.write("INI\r\n");
    QTimer::singleShot(100, this, [=]() {serialPortCTD.write("INI\r\n"); });
}


void DockWidgetCTD::on_m_radioButton_Hour_clicked(bool checked)
{
    if (checked)
    {
        ui->m_spinBox_Hour->setEnabled(checked);
        ui->m_spinBox_Minute->setEnabled(checked);
        ui->m_spinBox_Second->setEnabled(checked);
    }
}


void DockWidgetCTD::on_m_radioButton_Minute_clicked(bool checked)
{
    if (checked)
    {
        ui->m_spinBox_Hour->setEnabled(!checked);
    }
    ui->m_spinBox_Minute->setEnabled(checked);
    ui->m_spinBox_Second->setEnabled(checked);

}


void DockWidgetCTD::on_m_radioButton_Second_clicked(bool checked)
{
    if (checked)
    {
        ui->m_spinBox_Hour->setEnabled(!checked);
        ui->m_spinBox_Minute->setEnabled(!checked);
    }
}


void DockWidgetCTD::on_pushButtonStartSample_clicked()
{
    if (isStartSample)
    {
        uint iTotalSec = 0;
        if (ui->m_spinBox_Hour->isEnabled())
        {
            iTotalSec = ui->m_spinBox_Hour->value() * 3600;
        }
        if (ui->m_spinBox_Minute->isEnabled())
        {
            iTotalSec += ui->m_spinBox_Minute->value() * 60;
        }
        if (ui->m_spinBox_Second->isEnabled())
        {
            iTotalSec += ui->m_spinBox_Second->value();
        }
        if (iTotalSec <= 0)
        {
            QMessageBox::information(this, "提示信息", "时间间隔小于等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        if (iTotalSec >= 86399)
        {
            QMessageBox::information(this, "提示信息", "最大采样间隔24小时", QMessageBox::Ok);
            return;
        }
        // INTERVAL=3
        serialPortCTD.write("INTERVAL=" + QByteArray::number(iTotalSec) + "\r\n");
        // QTimer::singleShot(100, this, [=]() {serialPortCTD.write("INTERVAL=" + QByteArray::number(iTotalSec) + "\r\n"); });

    }
    // 发送停止采样指令
    else
    {
        serialPortCTD.write("STP\r\n");
        QTimer::singleShot(100, this, [=]() {serialPortCTD.write("STP\r\n"); });
    }


    
}



void DockWidgetCTD::on_mUseLastConfig_clicked()
{
    serialPortCTD.write("DCI\r\n");
    QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DCI\r\n"); });
}


void DockWidgetCTD::on_mCTDIMDownLoad_clicked()
{
    // QTimer::singleShot(100, this, [=]() {serialPortCTD.write("DNLE\r\n"); });
    QString m_strFilePath = QFileDialog::getSaveFileName(this, tr("Save Downloading data"), "./", "DAT Files(*.dat)");
    if (m_strFilePath.isEmpty())
    {
        return;
    }
    // 设置文件名字
    m_clsFile.setFileName(m_strFilePath);

    CTDIMDownLoad = true;
    serialPortCTD.write("DNLE\r\n");
    QTimer::singleShot(100, this, [=]() {
        serialPortCTD.write("DNLE\r\n"); 
        });

    if (m_pBar == nullptr)
    {
        m_pBar = new QProgressDialog();
    }
    m_pBar->setWindowModality(Qt::ApplicationModal);
    m_pBar->show();
    m_pBar->setRange(0,100);
    m_pBar->setCancelButton(0);
    m_pBar->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowMinimizeButtonHint);
    m_pBar->setValue(0);
    m_pBar->setAutoClose(true);
    m_pBar->setAutoReset(true);
    m_pBar->setLabelText("Downloading......");

}

void DockWidgetCTD::recvChangeStatus(bool flag)
{
    // 更改本类按钮状态
    ui->getStatusButton->setEnabled(flag);
    ui->mCTDIMDownLoad->setEnabled(flag);
    ui->clearMemory->setEnabled(flag);
    ui->mUseLastConfig->setEnabled(flag);
    ui->saveRealtimeTransfer->setEnabled(flag);
    ui->saveTransFormat->setEnabled(flag);
    ui->saveStorage->setEnabled(flag);
    ui->saveBaudRate->setEnabled(flag);
    ui->getConfig->setEnabled(flag);
    ui->m_PtnSaveCoefficient->setEnabled(flag);
    ui->m_CalibrationCoefficient->setEnabled(flag);
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    ui->pushButtonStartSample->setEnabled(flag);
}

void DockWidgetCTD::on_m_CalibrationCoefficient_clicked()
{
    serialPortCTD.write("GETCC\r\n");
    QTimer::singleShot(100, this, [=]() {serialPortCTD.write("GETCC\r\n"); });
}


void DockWidgetCTD::on_m_PtnSaveCoefficient_clicked()
{
    
    if (ui->m_combomBox_Coefficient->currentIndex() == 0)
    {
        QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(0, 3)->text().toUtf8();
        strTemperature = "HCS11:" + strTemperature + "\r\n";
        sendCommand(strTemperature);
    }
    else if (ui->m_combomBox_Coefficient->currentIndex() == 1)
    {
        QByteArray strPressure = ui->m_tableWidget_CalibrationT->item(0, 3)->text().toUtf8();
        strPressure = "HCS21:" + strPressure + "\r\n";
        sendCommand(strPressure);
    }
    else if (ui->m_combomBox_Coefficient->currentIndex() == 2)
    {
        // qDebug() << "33333333333";
        QByteArray strPressure = ui->m_tableWidget_CalibrationT->item(0, 3)->text().toUtf8();
        strPressure = "HCS31:" + strPressure + "\r\n";
        sendCommand(strPressure);
    }

}

void DockWidgetCTD::sendCommand(QByteArray& data)
{
    serialPortCTD.write(data);
}


void DockWidgetCTD::dealTableE11()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(1, 3)->text().toUtf8();
    strTemperature = "HCS12:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}

void DockWidgetCTD::dealTableE12()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(2, 3)->text().toUtf8();
    strTemperature = "HCS13:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}

void DockWidgetCTD::dealTableE13()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(3, 3)->text().toUtf8();
    strTemperature = "HCS14:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}


void DockWidgetCTD::dealTableE21()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(1, 3)->text().toUtf8();
    strTemperature = "HCS22:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}

void DockWidgetCTD::dealTableE22()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(2, 3)->text().toUtf8();
    strTemperature = "HCS23:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}

void DockWidgetCTD::dealTableE23()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(3, 3)->text().toUtf8();
    strTemperature = "HCS24:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}


void DockWidgetCTD::dealTableE31()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(1, 3)->text().toUtf8();
    strTemperature = "HCS32:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}

void DockWidgetCTD::dealTableE32()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(2, 3)->text().toUtf8();
    strTemperature = "HCS33:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}

void DockWidgetCTD::dealTableE33()
{
    readCTDAll.clear();
    QByteArray strTemperature = ui->m_tableWidget_CalibrationT->item(3, 3)->text().toUtf8();
    strTemperature = "HCS34:" + strTemperature + "\r\n";
    sendCommand(strTemperature);
}